package dao

import (
	"fmt"
	"time"
)

type Role struct {
	ID         int64 `gorm:"primary_key;not_null;auto_increment" json:"id"`
	Name       string
	CreateTime time.Time `gorm:"comment:'创建时间'" json:"createTime"`
	UpdateTime time.Time `gorm:"comment:'修改时间'" json:"updateTime"`
}

func (c *Role) TableName() string {
	return "role"
}

//func SelectById(id int) Role {
//	role := Role{}
//	DB.Debug().Where("id = ?", id).First(&role)
//	return role
//}

func (c *Role) Add() error {
	result := DB.Table(c.TableName()).Create(c)
	fmt.Printf("影响的行数：%d", result.RowsAffected)
	return result.Error
}

func (c *Role) Update(role *Role) error {
	return DB.Model(role).Update("name", role).Error
}

func (c *Role) DeleteById(id int64) error {
	return DB.Where("id = ?", id).Delete(&Role{}).Error
}

func (c *Role) FindByID(id int64) (role *Role, err error) {
	role = &Role{}
	return role, DB.First(role, id).Error
}
